package com.yb.web.controller.model;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yb.framework.core.domain.AjaxObject;
import com.yb.framework.core.domain.PageResult;
import com.yb.framework.core.utils.StringUtil;
import com.yb.framework.log.annotation.Log;
import com.yb.framework.log.enums.OperateTypeEnum;
import com.yb.module.model.domain.ModelShape;
import com.yb.web.controller.base.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/model/shape")
@Api(tags = "单元管理")
public class ModelShapeController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(ModelShapeController.class.getName());
	
	private static String MODEL_SHAPE = "model/shape/shape";
	private static String MODEL_SHAPE_ADD = "model/shape/shape_add";
	private static String MODEL_SHAPE_EDIT = "model/shape/shape_edit";
	private static String MODEL_NEO4J_CHART = "model/neo4j/chart";
	
	@GetMapping()
	@PreAuthorize("hasAuthority('model:shape')")
	public String main() {
		return MODEL_SHAPE;
	}
	
	@GetMapping("/add_view")
	@PreAuthorize("hasAuthority('model:shape:add')")
	public String addView() {
		return MODEL_SHAPE_ADD;
	}
	
	@GetMapping("/edit_view/{id}")
	@PreAuthorize("hasAuthority('model:shape:edit')")
	public String editView(@PathVariable("id") Long id, ModelMap model) {
		model.put("modelShape", modelShapeService.getById(id));
		return MODEL_SHAPE_EDIT;
	}
	
	@GetMapping("/chart/{id}")
	@PreAuthorize("hasAuthority('model:shape:chart')")
	public String chart(@PathVariable("id") Long id, ModelMap model) {
		model.put("label", "shape");
		model.put("nodeUuid", id);
		return MODEL_NEO4J_CHART;
	}
	
	/**
	 * 单元列表
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('model:shape:list')")
	@ApiOperation(value="分页查询", notes="分页查询单元信息")
	public @ResponseBody PageResult list(ModelShape modelShape) {
		return getPageInfo(modelShapeService.getPageByEntity(modelShape));
	}
	
	/**
	 * 新增单元
	 */
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('model:shape:add')")
	@ApiOperation(value="新增单元", notes="新增单元信息")
	@Log(label = "新增单元", operateType = OperateTypeEnum.ADD)
	public @ResponseBody AjaxObject add(ModelShape modelShape) {
		return toAjax(modelShapeService.insertShape(modelShape), "新增单元" + "[" + modelShape.getName() + "]");
	}
	
	/**
	 * 编辑单元
	 */
	@PostMapping("/edit")
	@PreAuthorize("hasAuthority('model:shape:edit')")
	@ApiOperation(value="编辑单元", notes="编辑单元信息")
	@Log(label = "编辑单元", operateType = OperateTypeEnum.EDIT)
	public @ResponseBody AjaxObject edit(ModelShape modelShape) {
		return toAjax(modelShapeService.updateShape(modelShape), "编辑单元" + "[" + modelShape.getName() + "]");
	}
	
	/**
	 * 删除单元
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('model:shape:delete')")
	@ApiOperation(value="删除单元", notes="批量删除单元信息")
	@Log(label = "删除单元", operateType = OperateTypeEnum.DELETE)
	public @ResponseBody AjaxObject delete(@RequestParam("ids") String ids) {
		List<String> modelShapeNames = modelShapeService.getNamesByIds(StringUtil.str2LongArray(ids));
		return toAjax(modelShapeService.deleteShape(StringUtil.str2LongArray(ids)), "批量删除单元" + modelShapeNames);
	}
	
	/**
	 * 单元下拉框
	 */
	@RequestMapping("/combo")
	public @ResponseBody List<ModelShape> combo() {
		return modelShapeService.getAll();
	}
}